package school.xauat.offershow.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import school.xauat.offershow.pojo.Entry;
import school.xauat.offershow.pojo.LoginResponse;
import school.xauat.offershow.pojo.User;
import school.xauat.offershow.pojo.dto.EntryResponseType;
import school.xauat.offershow.serice.EntryService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ：zsy
 * @date ：Created 2021/12/16 0:34
 * @description：
 */
@RestController
@Api(tags = {"条目Controller"})
@RequestMapping("/entry")
public class EntryController {

    @Autowired
    EntryService entryService;

    @GetMapping("/{pageId}/list")
    @ApiOperation("获取所有条目")
    @ResponseBody
    public List<EntryResponseType> searchAll(
            @PathVariable() Integer pageId,
            @RequestParam(defaultValue = "publish_time") String order) {
        List<EntryResponseType> list = entryService.list(pageId, order);
        return list;
    }

    @GetMapping("/{col}/searchByCol")
    @ResponseBody
    public List<EntryResponseType> searchByCol(
            @PathVariable String col,
            @RequestParam String value,
            @RequestParam(defaultValue = "publish_time") String order,
            @RequestParam(defaultValue = "1") Integer pageId) {
        return entryService.searchByCol(col, value, order, pageId);
    }

    @GetMapping("/searchByParams")
    public List<EntryResponseType> searchByParams(
            @RequestParam(defaultValue = "-1") Integer business_id,
            @RequestParam(defaultValue = "-1") Integer education_id,
            @RequestParam(defaultValue = "publish_time") String order,
            @RequestParam(defaultValue = "1") Integer pageId) {
        return entryService.searchByParams(business_id, education_id, pageId, order);
    }


    @GetMapping("/{entry_id}/searchById")
    public EntryResponseType searchById(
            @PathVariable Integer entry_id,
            HttpSession session) {
        int user_id = -1;
        if (session.getAttribute("user") != null) {
            User user = (User) session.getAttribute("user");
            user_id = user.getId();
        }
        return entryService.getById(user_id, entry_id);
    }

    @PostMapping("/add")
    @ApiOperation("添加一个条目")
    public LoginResponse add(@RequestBody Entry entry,
                             HttpSession session) {
        LoginResponse response = new LoginResponse();
        if (session.getAttribute("user") == null) {
            response.setSuccess(false);
            response.setMessage("当前状态:未登录");
        } else {
            entry.setUserId(((User) session.getAttribute("user")).getId());
            response.setSuccess(entryService.add(entry));
        }
        return response;
    }

    @GetMapping("/searchByUser")
    public LoginResponse searchByUserId(
            HttpSession session,
            @RequestParam(defaultValue = "publish_time") String order) {
        LoginResponse response = new LoginResponse();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            response.setSuccess(false);
            response.setReason("当前状态:未登录");
        } else {
            int userId = user.getId();
            List<EntryResponseType> list = entryService.searchByUserId(userId, order);
            response.setSuccess(true);
            response.setMessage(list);
        }
        return response;
    }

    @GetMapping("/{entry_id}/collect")
    public LoginResponse collect(
            @PathVariable Integer entry_id,
            HttpSession session) {
        LoginResponse response = new LoginResponse();
        if (session.getAttribute("user") == null) {
            response.setSuccess(false);
            response.setReason("请先登录");
        } else {
            User user = (User) session.getAttribute("user");
            int insert = entryService.addStars(user.getId(), entry_id);
            if (insert == 1) {
                response.setSuccess(true);
            } else {
                response.setSuccess(false);
                response.setReason("未知错误");
            }
        }
        return response;
    }

    @GetMapping("/{entry_id}/cancelCollect")
    public LoginResponse cancelCollect(
            @PathVariable Integer entry_id,
            HttpSession session) {
        LoginResponse response = new LoginResponse();
        if (session.getAttribute("user") == null) {
            response.setSuccess(false);
            response.setReason("请先登录");
        } else {
            User user = (User) session.getAttribute("user");
            int delete = entryService.cancelStars(user.getId(), entry_id);
            if (delete == 1) {
                response.setSuccess(true);
            } else {
                response.setSuccess(false);
                response.setReason("未知错误");
            }
        }
        return response;
    }



}